package cn.piggy.controller;


import cn.piggy.quartz.handler.JobHandler;
import cn.piggy.model.JobInfo;
import cn.piggy.quartz.task.JobTask;
import cn.piggy.quartz.task.TestTask;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.UUID;


@Controller
public class QuartzController {
    private final Logger logger = LoggerFactory.getLogger(TestTask.class);
    @Resource
    private JobHandler jobHandler;
    @GetMapping("/add1")
    @ResponseBody
    public JobInfo addJob1() throws SchedulerException {
        JobInfo jobInfo = new JobInfo();
        jobInfo.setJobGroup("jobGroup");
        jobInfo.setJobName("jobName");
        jobInfo.setClassType(JobTask.class);
        jobInfo.setTriggerGroup("triggerGroup");
        jobInfo.setTriggerName("triggerName");
        jobInfo.setConfig("config");
        jobInfo.setCron("0 0/1 * * * ?");
        jobHandler.addJob(jobInfo);
        return jobInfo;
    }

    @GetMapping("/delete1")
    @ResponseBody
    public String deleteJob1() throws SchedulerException {
        jobHandler.deleteJob("jobGroup", "jobName");
        return "success";
    }

    @GetMapping("/add2")
    @ResponseBody
    public String addJob2(String s) throws SchedulerException {
        logger.info("--> start");
        for(int i=0; i<2000; i++){
            JobInfo jobInfo = new JobInfo();
            jobInfo.setJobGroup("testJobGroup");
            jobInfo.setJobName("testJobName"+ UUID.randomUUID());
            jobInfo.setClassType(TestTask.class);
            jobInfo.setTriggerGroup("testTriggerGroup");
            jobInfo.setTriggerName("testTriggerName"+ UUID.randomUUID());
            jobInfo.setConfig("config");
            jobInfo.setCron("0 "+ s +" 18 14 4 ? 2024");
            jobHandler.addJob(jobInfo);
        }
//        for(int i=0; i<2000; i++){
//            JobInfo jobInfo = new JobInfo();
//            jobInfo.setJobGroup("testJobGroup");
//            jobInfo.setJobName("testJobName"+ UUID.randomUUID());
//            jobInfo.setClassType(TestTask.class);
//            jobInfo.setTriggerGroup("testTriggerGroup");
//            jobInfo.setTriggerName("testTriggerName"+ UUID.randomUUID());
//            jobInfo.setConfig("config");
//            jobInfo.setCron("0 1 18 14 4 ? 2024");
//            jobHandler.addJob(jobInfo);
//        }
        logger.info("--> finish");
        return "success";
    }
}
